﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ThousandFunClient
{
  public sealed class LoadSettingsCollection : ConfigurationElementCollection  
    {  
        private IDictionary<string, bool> settings;  
 
        protected override ConfigurationElement CreateNewElement()  
        {  
            return new LoadSettingsElement();  
        }  
 
        protected override object GetElementKey(ConfigurationElement element)  
        {  
            LoadSettingsElement ep = (LoadSettingsElement)element;  
 
            return ep.Key;  
        }  
 
        protected override string ElementName  
        {  
            get 
            {  
                return base.ElementName;  
            }  
        }  
 
        public IDictionary<string, bool> Settings  
        {  
            get 
            {  
                if (settings == null)  
                {  
                    settings = new Dictionary<string, bool>();  
                    foreach (LoadSettingsElement e in this)  
                    {  
                        settings.Add(e.Key, e.Value);  
                    }  
                }  
                return settings;  
            }  
        }  
 
        public bool this [string key]  
        {  
            get 
            {  
                bool isLoad = true;  
                if (settings.TryGetValue(key, out isLoad))  
                {  
                    return isLoad;  
                }  
                else 
                {  
                    throw new ArgumentException("没有对'" + key + "'节点进行配置。");  
                }  
            }  
        }  
 
    }  
 
    public class LoadSettingsElement : ConfigurationElement  
    {  
        [ConfigurationProperty("key", IsRequired = true)]  
        public string Key  
        {  
            get { return (string)base["key"]; }  
            set { base["key"] = value; }  
        }  
        [ConfigurationProperty("value", IsRequired = true)]  
        public bool Value  
        {  
            get { return (bool)base["value"]; }  
            set { base["value"] = value; }  
        }  
    }  
 
    public class PathSettingElement : ConfigurationElement  
    {  
        /// <summary>  
        ///   
        /// </summary>  
        [ConfigurationProperty("SavePath", IsRequired = true)]  
        public string SavePath  
        {  
            get { return (string)base["SavePath"]; }  
            set { base["SavePath"] = value; }  
        }  
        /// <summary>  
        ///   
        /// </summary>  
        [ConfigurationProperty("SearchSubPath", IsRequired = false, DefaultValue = true)]  
        public bool SearchSubPath  
        {  
            get { return (bool)base["SearchSubPath"]; }  
            set { base["SearchSubPath"] = value; }  
        }  
    }  
 
    /// <summary>  
    /// 对应config文件中的  
    /// </summary>  
    public sealed class SoftwareSettings : ConfigurationSection  
    {  
        /// <summary>  
        /// 对应SoftwareSettings节点下的LoadSettings子节点  
        /// </summary>  
        [ConfigurationProperty("LoadSettings", IsRequired = true)]  
        public LoadSettingsCollection LoadSettings  
        {  
            get { return (LoadSettingsCollection)base["LoadSettings"]; }  
        }  
 
        /// <summary>  
        /// 对应SoftwareSettings节点下的PathSettings子节点，非必须  
        /// </summary>  
        [ConfigurationProperty("PathSettings", IsRequired = false)]  
        public PathSettingElement PathSetting  
        {  
            get { return (PathSettingElement)base["PathSettings"]; }  
            set { base["PathSettings"] = value; }  
        }  
 
    }  
}
